package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import com.zzyl.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ContractMapper;
import com.zzyl.nursing.domain.Contract;
import com.zzyl.nursing.service.IContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;

/**
 * 签约办理Service业务层处理
 *
 * @author FuJiaWei
 * @date 2025-05-23
 */
@Slf4j
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements IContractService {
    @Autowired
    private ContractMapper contractMapper;

    /**
     * 查询签约办理
     *
     * @param id 签约办理主键
     * @return 签约办理
     */
    @Override
    public Contract selectContractById(Long id) {
        return getById(id);
    }

    /**
     * 查询签约办理列表
     *
     * @param contract 签约办理
     * @return 签约办理
     */
    @Override
    public List<Contract> selectContractList(Contract contract) {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增签约办理
     *
     * @param contract 签约办理
     * @return 结果
     */
    @Override
    public int insertContract(Contract contract) {
        return save(contract) ? 1 : 0;
    }

    /**
     * 修改签约办理
     *
     * @param contract 签约办理
     * @return 结果
     */
    @Override
    public int updateContract(Contract contract) {
        return updateById(contract) ? 1 : 0;
    }

    /**
     * 批量删除签约办理
     *
     * @param ids 需要删除的签约办理主键
     * @return 结果
     */
    @Override
    public int deleteContractByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除签约办理信息
     *
     * @param id 签约办理主键
     * @return 结果
     */
    @Override
    public int deleteContractById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public void updateContractStatus() {
        LocalDateTime now = LocalDateTime.now();
        List<Contract> contractList = lambdaQuery().in(Contract::getStatus, 0, 1).list();
        log.info("list大小" + contractList.size());
        List<Contract> upList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(contractList)) {
            contractList.forEach(contract -> {
                Integer status = contract.getStatus() == 0 ? (contract.getStartDate().isBefore(now) ? contract.getEndDate().isBefore(now) ? (3) : (1) : 0)
                        :
                        (contract.getEndDate().isBefore(now) ? 2 : 1);
                if (!contract.getStatus().equals(status)) {
                    Contract upContract = new Contract();
                    upContract.setId(contract.getId());
                    upContract.setStatus(status);
                    upList.add(upContract);
                }
            });
        }
        if (!CollectionUtils.isEmpty(upList)) {
            updateBatchById(upList);
        }
    }
}
